import { createApp } from 'vue'
import { Button, Input,  Layout,Row,Col,Checkbox, Icon, Divider,
  TextArea,List, DatePicker, Picker,Switch,Popup,OverLay, Tag,Avatar,
   Form, FormItem, Cell, Navbar,  CellGroup, Notify,Radio,RadioGroup, Menu, MenuItem,
   Address,
   Toast, SearchBar, Tabs, TabPane, Uploader, Dialog, Tabbar, TabbarItem } from '@nutui/nutui-taro'
import Taro from '@tarojs/taro';
import './app.scss' 
 

import { createStore } from 'vuex' 
import EventBus from './utils/event.js'
import format from './utils/format.js'
const $bus = new EventBus()

const store = createStore({ // https://mp.weixin.qq.com/s?src=11&timestamp=1650074859&ver=3741&signature=B4veyKkH-7hzE8cb3oUHzNXl0aaBHscNDTxiVtw5Jx6AWU443SZ4TNG0joch4z2Qkf59nM3auQhjko0FD1dtDScMdifZvIT4ZanTJEl42h-Pucnst4JH1iit33Vd9k2y&new=1
  state() { 
    return {
      count:0,
      wxUserInfo:{},
      apiUserInfo:{
        name:"wzskyline"
      },
      mapInfo:{},
      publishInfo:{},
      ztLocation:{},
      ztSpace:{},
      ztPrice:{}, 
      tmp:{},
    }
  },
  modules: {},
  mutations: {
    addCount(state, data) {  state.count+=data },
    asyncwxUserInfo(state, data) {  state.wxUserInfo = Object.assign(state.wxUserInfo,data)  },
    asyncApiUserInfo(state, data) {  state.apiUserInfo = Object.assign(state.apiUserInfo,data)  },
    asyncPublishInfo(state, data) {  state.publishInfo = Object.assign(state.publishInfo,data)  },
    asyncMapInfo(state, data) {  state.mapInfo = Object.assign(state.mapInfo,data)  },
    asyncTmp(state, data) {  state.tmp = Object.assign(state.tmp,data)  },
    asyncztLocation(state, data) {  state.ztLocation = Object.assign(state.ztLocation,data)  },
    asyncztSpace(state, data) {  state.ztSpace = Object.assign(state.ztSpace,data)  },
    asyncztPrice(state, data) {  state.ztPrice = Object.assign(state.ztPrice,data)  },
    
  },
  actions: {
    addAction({ commit },data){  commit('addCount',data) },
    asyncwxUserInfo({ commit },data){  commit('asyncwxUserInfo',data) },
    asyncApiUserInfo({ commit },data){  commit('asyncApiUserInfo',data) },
    asyncPublishInfo({ commit },data){  commit('asyncPublishInfo',data) },
    asyncMapInfo({ commit },data){  commit('asyncMapInfo',data) },
    asyncTmp({ commit },data){  commit('asyncTmp',data) },
    ztLocation({ commit },data){  commit('asyncztLocation',data) },
    ztSpace({ commit },data){  commit('asyncztSpace',data) },
    ztPrice({ commit },data){  commit('asyncztPrice',data) },

  },
  
  getters: {
    getCount(state){
      return `我是前端仔，在测试getters方法,获取count的值是：${state.count}`
    },
    getApiUserInfo(state){
      return state.apiUserInfo
    },
    getMapInfo(state){
      return state.mapInfo
    },
  }
})
 
const App = createApp({
  onLaunch() { 

  },
  onShow(options) {}
  // 入口组件不需要实现 render 方法，即使实现了也会被 taro 所覆盖
}) 
App.config.globalProperties.toPage = (url)=>{ Taro.navigateTo({url})};
App.config.globalProperties.params = ()=>{ return Taro.getCurrentInstance().router.params };
App.config.globalProperties.$bus = $bus
App.config.globalProperties.format = format

App.config.globalProperties.token  = 'Bearer ' + "";
let token = ''
App.config.globalProperties.ajax = {
  api:{
    wxLogin:  '/user/wxLogin',
    queryBuildingsByPropertyAddress: `/location/queryBuildingsByPropertyAddress`,
    querySpaceByBuildinglocat:'ion/querySpaceByBuilding', 
    fileUpload:'https://112.74.74.239/prod-api/wx/oss/upload',
    
    property:'/property',
    propertys:'/property/list',
    listByAuth:'/property/listByAuth',
    propertyDel:'/property/id',
    propertyDatil:'/property/id',
    addTemp:'/property/addTemp',
    loadTemp:'/property/loadTemp', 
    getUserInfo:'/user/getInfo',
    genCode:'/invitationCode/genCode',
    updateUser:'/user/updateUser',
    submitInvitationCode:'/user/submitInvitationCode',  
  },
  baseOptions(params, method = 'GET') { 
    console.log('params', params) 
    return Taro.request({
      isShowLoading: false,
      loadingText: '正在加载',
      url: "https://112.74.74.239/prod-api/wx" + params.url,
      data: params.data,
      method: method,
      header: { 'content-type':  params.contentType ||  'application/json', 'Authorization': App.config.globalProperties.token  },
       success(res) {
         if(params.url.includes("wxLogin")){
          console.log("登录后的响应", res);
          console.log("登录后的响应", res.data.data);
           var token ='Bearer ' + res.data.data.token;
           console.log("登录后的响应 TOKEN= ", token);
           Taro.setStorageSync('token',token)
           App.config.globalProperties.token = token
         
         }
         return res.data
      },
      error(e) {
        console.log('api', '请求接口出现问题', e)
      }
    })
  },
  get(url, data = '') { 
    return this.baseOptions({ url, data })
  },
  post: function (url, data) { 
    return this.baseOptions({ url, data }, 'POST')
  },
  upload: function (url, data) {  
     return this.baseOptions({ url, data, contentType:'multipart/form-data' }, 'POST')
  }
}
App.use(store).use(Button)
  .use(Input)
  .use(Checkbox)
  .use(Icon)
  .use(Button)
  .use(Form)
  .use(FormItem)
  .use(Cell)
  .use(CellGroup)
  .use(Notify)
  .use(Toast)
  .use(SearchBar)
  .use(Tabs)
  .use(TabPane)
  .use(Uploader)
  .use(Dialog)
  .use(Tabbar)
  .use(TabbarItem)
  .use(Navbar)  
  .use(Layout)
  .use(Row) 
  .use(Col)  
  .use(TextArea)   
  .use(Radio)   
  .use(RadioGroup)   
  .use(List)   
  .use(DatePicker)   
  .use(Picker)   
  .use(Switch) 
  .use(Popup)   
  .use(OverLay)   
  .use(Tag)   
  .use(Menu)   
  .use(MenuItem)    
  .use(Address)     
  .use(Avatar)    
  .use(Divider)    
   
 
export default App
